import { BaseState } from './BaseState';

export class BaseStateMachine {
    protected _prevState: BaseState = null;
    protected _currentState: BaseState = null;
    protected _curStateType: string;
    protected _stateMap: Map<string, BaseState> = new Map();
    protected _isOn : boolean = false;
    public get isOn() : boolean {
        return this._isOn;
    }
    public set isOn(v : boolean) {
        this._isOn = v;
        if (this._isOn) {
            this.nextState();
        }else{
            if (this._currentState) {
                this._currentState.onExit();
            }
            this._currentState = null;
            this._curStateType = null;
        }
    }
    
    registState(stateType: string, state: BaseState) {
        if (this._stateMap.has(stateType) == false) {
            this._stateMap.set(stateType, state);
        }
    }

    onUpdate(deltaTime: number) {
        if (this._isOn) {
            if (this._currentState) {
                this._currentState.onUpdate(deltaTime);
            }
        }
    }

    changeState(stateType: string) {
        if (this._stateMap.has(stateType)) {
            var state = this._stateMap.get(stateType);
            if (state) {
                if (this._currentState) {
                    this._currentState.onExit();
                    console.log("onExit =>" + this._curStateType);
                }
                this._curStateType = stateType;
                this._currentState = state;
                this._currentState.onEnter();
                console.log("onEnter =>" + this._curStateType);
            }
        }
    }

    nextState(){}
}


